﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _08_Lock锁
{
    class Account
    {
        /// <summary>
        /// 余额
        /// </summary>
        public int Blance { get; set; }
        public Account(int blance)
        {
            Blance = blance;
        }
        /// <summary>
        /// 取款方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="amount"></param>
        public void withDraw(string name, int amount)
        {
            lock (this)
            {
                if (this.Blance > amount)//取款金额要大于余额
                {
                    Thread.Sleep(100);//模拟取款时间
                    this.Blance -= amount;
                    Console.WriteLine(name + "取款成功！余额为:" + this.Blance);
                }
                else
                {
                    Console.WriteLine(name + "取款失败！余额不足");
                }                
            }


        }
    }
    internal class Program 
    {
        static void Main(string[] args)
        {
            Account account = new Account(1000);
            new Thread(() => account.withDraw("小王", 600)) { IsBackground = true }.Start();
            new Thread(() => account.withDraw("小李", 600)) { IsBackground = true }.Start();
            Console.ReadKey();
        }
    }
}
